package EGS.impl;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;

import PRODUCT.PACKAGE.c_package;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service10009Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 10009 新建包装箱
	 */
	public EGS_execute_service10009Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("10009")) {// 新建包装箱
				String iNumber = Mroot.element("body").element("product").attribute("number").getValue();
				String iVersion = Mroot.element("body").element("product").attribute("version").getValue();
				String iBIN = Mroot.element("body").element("product").attribute("bin").getValue();
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleA = eleB.addElement("packageContainer");
				try {
					c_package c = new c_package(sql);
					String boxNumber = c.newBox(iNumber, iVersion, iBIN, iStation);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					eleA.addAttribute("number", boxNumber);
					AKSLogs(100091, iStation + "_newBox=" + iNumber + "_" + iVersion + "_" + iBIN);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					eleA.addAttribute("number", "NA");
					AKSLogs(100090, iStation + "_newBox=" + iNumber + "_" + iVersion + "_" + iBIN);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
